﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiagramDesigner.BusinessModelObject;

namespace DiagramDesigner
{
    /// <summary>
    /// Interaction logic for CustomCommandUse.xaml
    /// </summary>
    public partial class GUILevel
    {
        public GUILevel()
        {
            InitializeComponent();
            lstview.ItemsSource = Utils.MeasureAttribute.StockCheck();
            lstview.SelectionChanged += ItemSelectionChanged;
            lstview.KeyUp += new KeyEventHandler(lstview_KeyUp);

            //imgSource.MouseLeftButtonDown += imgSourceMouseDown;
            imgSource.MouseDown += imgSourceMouseDown;
        }
        public string Criterio
        {
            get;
            set;
        }

        void lstview_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Subtract)
            {
                //MessageBox.Show("Evento");
                eliminarFila();
                OrganizarColumnas();
            }
            //MessageBox.Show(e.Key.ToString());
            //throw new NotImplementedException();
        }    
        
        private void OnAddListItemExecute(object sender, ExecutedRoutedEventArgs e)
        {
            ListView lv = sender as ListView;
            if (lv != null)
            {
                lv.Items.Add(e.Parameter);
                //txtItem.Clear();
                e.Handled = true;
            }
        }

        private void OnRemoveListItemExecute(object sender, ExecutedRoutedEventArgs e)
        {
            ListView lv = sender as ListView;
            if (lv != null)
            {
                if (lv.SelectedItem != null)
                {
                    lv.Items.Remove(lv.SelectedItem);
                    e.Handled = true;
                }
            }
        }

        private void CanAddListItemExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;// !String.IsNullOrEmpty(txtItem.Text) && !listview.Items.Contains(txtItem.Text);
            e.Handled = true;
        }

        private void CanRemoveListItemExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            ListView lv = sender as ListView;
            if (lv != null)
            {
                e.CanExecute = lv.SelectedItem != null;
                e.Handled = true;
            }
        }

        private void lstview_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //ListView lv = sender as ListView;
            //GridView gv = lv.View as GridView;

            //if (((DockPanel)((Grid)lv.Parent).Parent).RenderSize.Width - 40 > 0)
            //{
            //    gv.Columns[1].Width = ((DockPanel)((Grid)lv.Parent).Parent).RenderSize.Width - 40;
            //}
            //else
            //{
            //    gv.Columns[1].Width = 0; 
            //}
            OrganizarColumnas();
        }

        public void eliminarFila()
        {
            if (lstview.SelectedItem != null)
            {
                //lv.Items.Remove(lv.SelectedItem);
                List<Utils.MeasureAttribute> CurrentProducts = (List<Utils.MeasureAttribute>)lstview.ItemsSource;
                List<Utils.MeasureAttribute> NewProducts = new List<DiagramDesigner.Utils.MeasureAttribute>();
                foreach (Utils.MeasureAttribute dr in CurrentProducts)
                {
                    if (dr != lstview.SelectedItem)                   
                    {
                        NewProducts.Add(dr);
                    }
                }

                //Atributos
                DesignerItem dItem = (DesignerItem)((GUILevel)((DockPanel)((Grid)borderHeader.Parent).Parent).Parent).Parent;
                
                MainElementModel onk = (MainElementModel)dItem.Item;
                Dictionary<int, BaseField> _atributos = onk.Atributos;

                Dictionary<int, BaseField> _atributosFinal = new Dictionary<int, BaseField>();

                int contador = 0;

                int intInicio = 0;
                int intFinal = _atributos.Count;
                int intIndice = lstview.SelectedIndex;

                if (_atributos.ContainsKey(-1))
                {
                    intInicio = -1;
                    intFinal = _atributos.Count - 1;
                    contador = -1;
                }

                Dictionary<int, BaseField>.KeyCollection keys1 = _atributos.Keys;
                

                for (int k = intInicio; k < intFinal; k++)
                {
                    if (k != lstview.SelectedIndex)
                    {
                        _atributosFinal[contador] = _atributos[k];
                        contador = contador + 1;
                    }
                }

                onk.Atributos.Clear();

                intInicio = 0;
                intFinal = _atributosFinal.Count;

                if (_atributosFinal.ContainsKey(-1))
                {
                    intInicio = -1;
                    intFinal = _atributosFinal.Count - 1;
                }
                for (int k = intInicio; k < intFinal; k++)
                {
                    onk.Atributos.Add(k, _atributosFinal[k]);
                }

                lstview.ItemsSource = NewProducts;
                lstview.Items.Refresh();
            }
        }

        public void OrganizarColumnas()
        {
            GridView gv = lstview.View as GridView;
            bool oneColumn = true;

            List<Utils.MeasureAttribute> CurrentProducts = (List<Utils.MeasureAttribute>)lstview.ItemsSource;
            foreach (Utils.MeasureAttribute dr in CurrentProducts)
            {
                if (dr.ElementType != string.Empty)
                {
                    oneColumn = false;
                }
            }

            double ancho = ((DockPanel)((Grid)lstview.Parent).Parent).RenderSize.Width;
            if (oneColumn)
            {
                gv.Columns[1].Width = ancho;
                gv.Columns[0].Width = 0;                
            }
            else
            {
                if (ancho - 30 > 0)
                {
                    gv.Columns[1].Width = ancho - 30;
                }
                else
                {
                    gv.Columns[1].Width = 0;
                }
                gv.Columns[0].Width = 30;                
            }
        }

        public void OrganizarCabecera()
        {
            // Grid gv = sender as Grid;
            double ancho = ((DockPanel)((Grid)((Border)gvCabecera.Parent).Parent).Parent).RenderSize.Width - 8;


            if (imgSource.Source.ToString().Contains("blank.png"))
            {
                lblName.Width = ancho;
                imgSource.Width = 0;
            }
            else
            {
                if (ancho - 30 > 0)
                    lblName.Width = ancho - 30;
                else
                    lblName.Width = 0;
                imgSource.Width = 30;
            }  
        }

        private void gvCabecera_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            OrganizarCabecera();
        }

        private void DockPanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DockPanel dcPanel = sender as DockPanel;
            gvCabecera.Width = dcPanel.RenderSize.Width;

            gvCabecera_SizeChanged(null, null);
        }

    }
}
